﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;



public interface IPackItem
{
    public string Name { get; }
    public string Type { get; }
    public bool IsText { get; }
    public bool IsGroup { get; }
    public byte[] Bytes { get; }
    public string GetAsText();
}
public interface IPackGroup : IPackItem
{
    //得到所有资源
    string[] GetAllResourceName();
    IPackItem GetPackItem(string name);



}
public class PackGroup_Item : IPackItem
{
    public string Name
    {
        get;
        set;
    }

    public string Type
    {
        get;
        set;
    }

    public bool IsText
    {
        get
        {
            return this.Type == ".json" || this.Type == ".js" || this.Type == ".txt";
        }
    }

    public bool IsGroup => false;

    public byte[] Bytes
    {
        get;
        set;
    }

    public string GetAsText()
    {
        if (!IsText)
            throw new NotImplementedException();
        return System.Text.Encoding.UTF8.GetString(Bytes);
    }
}
public class PackGroup_Mem : IPackGroup
{
    public string Name
    {
        get;
        set;
    }

    public string Type
    {
        get;
        set;
    }

    public bool IsText => false;

    public bool IsGroup => true;

    public byte[] Bytes => throw new NotImplementedException();

    public Dictionary<string, IPackItem> allres = new Dictionary<string, IPackItem>();
    public string[] GetAllResourceName()
    {
        return allres.Keys.ToArray();
    }

    public string GetAsText()
    {
        throw new NotImplementedException();
    }

    public IPackItem GetPackItem(string name)
    {
        if (allres.TryGetValue(name, out IPackItem item))
        {
            return item;
        }
        return null;
    }
}

public class JSPackItem
{
    public IPackItem _item;
    protected JSPackItem(IPackItem item)
    {
        this._item = item;
    }
    public static JSPackItem _FromNative(IPackItem native)
    {
        if (native == null)
            return null;
        else
            return new JSPackItem(native);
    }
    public string Name
    {
        get
        {
            return _item.Name;
        }
    }
    public string Type
    {
        get
        {
            return _item.Type;
        }
    }
    public bool IsText
    {
        get
        {
            return _item.IsText;
        }
    }
    public bool IsGroup
    {
        get
        {
            return _item.IsGroup;
        }
    }
    public byte[] Bytes
    {
        get
        {
            return _item.Bytes;// new Puerts.ArrayBuffer(_item.Bytes);
        }
    }

    public string GetAsText()
    {
        return _item.GetAsText();
    }
}

public class JSPackGroup : JSPackItem
{
    private JSPackGroup(IPackGroup group) : base(group)
    {

    }
    public static JSPackGroup _FromNative(IPackGroup native)
    {
        if (native == null)
            return null;
        else
            return new JSPackGroup(native);
    }
    public TTArray<string> GetAllResourceName()
    {
        var names = (this._item as IPackGroup).GetAllResourceName();
        return TTArray<string>._FromNative(names);
    }
    Dictionary<string, JSPackItem> packItems = new Dictionary<string, JSPackItem>();
    public JSPackItem GetPackItem(string name)
    {
        if (packItems.TryGetValue(name, out var v))
        {
            return v;
        }
        {
            var _item = (this._item as IPackGroup).GetPackItem(name);
            var i = JSPackItem._FromNative(_item);
            packItems[name] = i;
            return i;
        }
        return null;
    }
}

